<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_system_attribute_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_system_attribute_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');


class VPage_System_Attribute_Del_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_vpage/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('vpage');
		$this->add_transit_var('category');
	}

	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$v2 = $this->create_view('not_exist');
		$this->add_view($v2);

		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('delete', Tangra_Parameter_Method::GET, 'delete'), 'delete_event');
		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('delete_confirmed', Tangra_Parameter_Method::GET, 'delete_confirmed'), 'delete_confirmed');
	}


	protected function delete_event($sa_id) {
		$dbc = $this->get_dbc();

		$vpage_id = $this->get_transit_var('vpage');
		$this->export('vpage_id', $vpage_id);

		$sa = new TCMS_VPage_System_Attribute_DBC();
		if ($sa->load_by_id($dbc, $sa_id)) {
			$t_sa = new TCMS_VPT_System_Attribute_DBC();
			$t_sa->load_by_id($dbc, $sa->get_t_sa_id());

			$this->export('t_sa_name', $t_sa->get_sa_name());
			$this->export('value', $sa->get_value());
			$this->export('sa_id', $sa_id);
			$view = $this->get_view('default');
		} else {
			$view = $this->get_view('not_exist');
		}

		return $view;
	}


	protected function delete_confirmed($sa_id) {
		$dbc = $this->get_dbc();
		try {
			$dbc->start_trans();
			$config = $this->get__config();
			TCMS_VPage_System_Attribute_DBC::delete($dbc, $sa_id);

			$vpage_id = $this->get_transit_var('vpage');
			if ($vpage_id) {
				TCMS_VPage_DBC::update_last_modified($dbc, $vpage_id, $this->get_user()->get_id());
			}

			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$redir = $this->create_redirect_view('admin/modules/tangra_cms/vpage/vpage_manage.php', array('manage' => $this->get_transit_var('vpage')));

		return $redir;
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Expecting "delete" parameter.');
	}
}